<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Api\CustomUsers\StoreController;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Nyholm\Psr7\Response;

class UserController extends Controller
{

    /**
     * @OA\Get(
     *     path="/api/user/get",
     *     tags={"auth"},
     *     summary="获取用户信息",
     *    security={
     *         {"authorization": {}}
     *     },
     *     @OA\Response(
     *         response=200,
     *         description="success",
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(
     *                     property="status",
     *                     type="integer",
     *                     format="int64",
     *                 ),
     *                 @OA\Property(
     *                     property="msg",
     *                     type="string",
     *                 ),
     *                 example={"status": 200, "data": "2019-03-21 09:00:00","msg": "success"}
     *             )
     *         )
     *     )
     * )
     */
    public function get(Request $request)
    {
        $user = $request->user();
        return $this->success($user);
    }


    /**
     * @OA\Get(
     *     path="/api/user/list",
     *     tags={"auth"},
     *     summary="获取所有用户信息",
     *    security={
     *         {"authorization": {}}
     *     },
     *     @OA\Response(
     *         response=200,
     *         description="success",
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(
     *                     property="status",
     *                     type="integer",
     *                     format="int64",
     *                 ),
     *                 @OA\Property(
     *                     property="msg",
     *                     type="string",
     *                 ),
     *                 example={"status": 200, "data": "2019-03-21 09:00:00","msg": "success"}
     *             )
     *         )
     *     )
     * )
     */
    public function list(Request $request)
    {
        $data = User::get()->toArray();
//        print_r($data);die;
        return $this->success($data);
    }

    /**
     * @OA\Post(
     *     path="/api/user/login",
     *     tags={"auth"},
     *     summary="登录",
     *    security={
     *         {"authorization": {}}
     *     },
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     * @OA\Property( property="email", type="string", description="email",default="brunolfsson@example.com"),
     * @OA\Property( property="password", type="string", description="password",default="password"),
     *                required={}
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="success"
     *     ),
     *     @OA\Response(
     *         response=103,
     *         description="信息不完善"
     *     ),
     *
     * )
     */
    public function login(Request $request)
    {
        $email = $request->input("email");
        $password = $request->input("password");

        $auth = auth()->attempt([
            'email' => $email,
            'password' => $password
        ]);
        if($auth){
            $user = User::where('email', $email)->first();
            $name = $user->name ?? '';
            return $this->proxy(
                'password',
                $name,
                [
                    'username'=> $email,
                    'password'=> $password,
                    'scope'=> '',
                ]
            );
        }
        return response()->json([
            'code'=> 501,
            'message'=>'账号密码错误'
        ]);
    }


    public function proxy($grantType, $name, array $data = []){
        $data = array_merge($data,[
            'client_id' => config('auth.guards.passport.client_id'),
            'client_secret' => config('auth.guards.passport.client_secret'),
            'grant_type'=> $grantType
        ]);
        $url = 'http://'.$_SERVER["HTTP_HOST"].'/oauth/token';

        $http = new \GuzzleHttp\Client();
        $response = $http->request('POST',$url,[
            'verify' => false,
            'form_params' => $data
        ]);
        $token = json_decode(( string) $response->getBody(),true);
//        print_r($token);die;

        return response()->json([
            'code' => 1,
            'data' => [
                'token' => $token['access_token'],
                'expires_in' => $token['expires_in'],
                'user'=>$name
            ]
        ])->cookie('refreshToken',$token['refresh_token'],14000,null,null,false,true);
    }

}
